<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动关闭广告</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            position: relative;
        }
        
        .img_lose {
            position: absolute;
            left: -450px;
            transition: 5s;
        }
        
        .content {
            font-size: 66px;
            color: fuchsia;
            position: absolute;
            top: 515px;
            left: 377px;
        }
    </style>
</head>

<body>
    <img src="./img/3.png" width="450px" alt="">
    <span class="content"></span>
    <br>
    <button id="btn">你过来呀</button>
    <button id="go">必须走</button>
</body>

</html>
<script>
    window.addEventListener('DOMContentLoaded', function(e) {
        let btn = window.document.querySelector('#btn');
        let go = window.document.querySelector('#go');
        let img = window.document.querySelector('img');
        let count = 15;
        let interval = null;
        let text = 15;

        function gogo(e) {

            if (count < 10) {
                text = '0' + count;
            }
            img.nextElementSibling.innerText = text;

            interval = setInterval(function() {
                text = --count;
                if (count < 10) {
                    text = '0' + count;
                }
                if (count == 0) {
                    img.nextElementSibling.innerText = '';
                    img.nextElementSibling.style.display = 'none';
                    clearInterval(interval);
                }
                img.nextElementSibling.innerText = text;
            }, 1000);
        };
        gogo();
        let timeout = null;

        function gotime(params) {
            timeout = setTimeout(function() {
                img.className = 'img_lose';
            }, 1000 * count);
        }
        gotime();
        btn.addEventListener('click', function(e) {
            clearInterval(interval);
            clearTimeout(timeout);
            img.nextElementSibling.innerText = '';
        });
        go.addEventListener('click', function() {
            gogo();
            gotime();
        });
    });
</script>